package com.example.teesystem.controller;

import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.FileUtil;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.common.valid.MapContains;
import com.example.teesystem.common.valid.ValidErrorMessage;
import com.example.teesystem.entity.vo.classStudent.ClassStudentUpdateVo;
import com.example.teesystem.entity.vo.classStudent.ClassStudentUploadVo;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.ClassService;
import com.example.teesystem.service.ClassStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/classStudent", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
@Validated
public class ClassStudentController {
    private final ClassStudentService classStudentService;
    private final ClassService classService;
    private final HttpServletResponse response;
    
    /**
     * 给班级批量添加学生
     * @param classStudentUploadVo 学生 id 和 班级 id
     * @return
     */
    @PostMapping("/add")
    public Response add(@Valid @RequestBody ClassStudentUploadVo classStudentUploadVo) {
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.CLASS_INFO_MANAGE,classService.getCourseId(classStudentUploadVo.getClassId()))) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        return classStudentService.add(classStudentUploadVo);
    }
    
    /**
     * 移除班级里的学生
     * @param classStudentUploadVo 学生 id 和 班级 id
     * @return
     */
    @PostMapping("/remove")
    public Response remove(@Valid @RequestBody ClassStudentUploadVo classStudentUploadVo) {
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.CLASS_INFO_MANAGE,classService.getCourseId(classStudentUploadVo.getClassId()))) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        if(classStudentService.remove(classStudentUploadVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.DELETE_INFO_FAIL);
        }
    }
    
    /**
     * 将学生从一个班级移到另一个班级
     * @param classStudentUpdateVo 学生 id 和 班级 id
     * @return
     */
    @PostMapping("/update")
    public Response update(@Valid @RequestBody ClassStudentUpdateVo classStudentUpdateVo) {
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(), PermissionUtil.CLASS_INFO_MANAGE, classService.getCourseId(classStudentUpdateVo.getCurClassId()))) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        if(classStudentService.update(classStudentUpdateVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.UPDATE_INFO_FAIL);
        }
    }
    
    /**
     * 查询一个班级的所有学生
     * @param map 班级 id
     * @return
     */
    @PostMapping("/get")
    public Response get(
            @MapContains(keys = {"classId"}, message = ValidErrorMessage.CLASS_ID_VALID_FAIL)
            @RequestBody Map<String, Integer> map) {
        Integer classId = map.get("classId");
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.CLASS_INFO_MANAGE,classService.getCourseId(classId))) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        return Response.ok(classStudentService.get(classId));
    }
    
    /**
     * 获取自己加入的班级
     * @return
     */
    @PostMapping("/getMyClass")
    public Response getMyClass() {
        return Response.ok(classStudentService.getMyClass());
    }
    
    /**
     *
     * @param map
     * @return
     */
    @PostMapping("/applyJoin")
    public Response applyJoin(
            @MapContains(keys = {"classId"}, message = ValidErrorMessage.CLASS_ID_VALID_FAIL)
            @RequestBody Map<String, Integer> map) {
        if(classStudentService.applyJoin(map.get("classId"))) {
            return Response.ok();
        } else {
            return Response.error(ResultCode.ADD_INFO_FAIL);
        }
    }

    /**
     * 教师获取学生信息
     * @param map
     * @return
     */
    @PostMapping("/getStudentMessage")
    public Response getStudentMessage(@RequestBody Map<String,Integer> map){
        Integer classId = map.get("classId"),studentId = map.get("studentId");
        if(classId == null || studentId == null){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        return classStudentService.getStudentMessage(classId,studentId);
    }

    /**
     * 教师更改学生帐号状态
     * @param map
     * @return
     */
    @PostMapping("/updateStudentAccountType")
    public Response updateStudentMessage(@RequestBody Map<String,Integer> map){
        Integer classId = map.get("classId"),studentId = map.get("studentId"),accountType = map.get("accountType");
        if(classId == null || studentId == null || accountType == null){
            return Response.error(ResultCode.PARAMETER_MISS);
        }
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.STUDENT_INFO_MANAGE,classService.getCourseId(classId))) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        return classStudentService.updateStudentAccountType(classId,studentId,accountType);
    }

    /**
     * 教师更改学生密码
     * @param map
     * @return
     */
    @PostMapping("/updateStudentPassword")
    public Response updateStudentPassword(@RequestBody Map<String,String> map){
        int classId,studentId;
        String password;
        try{
            classId = Integer.parseInt(map.get("classId"));
            studentId = Integer.parseInt(map.get("studentId"));
            password = map.get("password");
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(),PermissionUtil.STUDENT_INFO_MANAGE,classService.getCourseId(classId))) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        return classStudentService.updateStudentPassword(classId, studentId, password);
    }

    @PostMapping(value = "/downLoadExcel")
    public Response downLoadExcel(){
        try{
            FileUtil.downloadFileByWeb(FileUtil.STU_EXCEL,response);
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.FILE_DOWNLOAD_ERROR);
        }
    }
}
